﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Web.Mvc;
using AFT.Parking.Bizlogic.Parkings;
using AFT.Parking.Entity;
using AFT.Parking.Entity.ArgumentEntity;
using AFT.Parking.Foundation.Extensions;
using AFT.Parking.WebSite.Common;
using AFT.Parking.WebSite.Filter;
using Newtonsoft.Json;

namespace AFT.Parking.WebSite.Controllers
{
    [AdminOnly]
    public class MallAreasController : Controller
    {

        private readonly ParkingBiz _parkingBiz;
        public MallAreasController()
        {
            _parkingBiz = new ParkingBiz();
        }

        #region MallArea Manage
        public ActionResult AddFloor(int id)
        {
            var parking = _parkingBiz.GetSingle(id);
            if (parking == null)
                return Redirect("/parkingAreas/parkings");
            ViewBag.ParkingName = parking.Name;
            return View(new MallArea { IsEnable = true, ImgUrl = ConstFields.DefaultImage, ParkingId = id });
        }

        [HttpPost]
        public ActionResult AddFloor(MallArea mallArea)
        {
            var parking = _parkingBiz.GetSingle(mallArea.ParkingId);
            if (parking == null)
                return Redirect("/parkingAreas/parkings");

            string msg = string.Empty;
            string icon = "error";

            if (mallArea.Name.IsNullOrEmpty())
                msg = "楼层名称不能为空；";
            if (mallArea.ImgUrl.IsNullOrEmpty())
                msg += "图片不能为空。";
            if (msg.Length == 0)
            {
                mallArea.ImgUrl = FileHelper.Save("Areas", mallArea.ImgUrl);
                if (_parkingBiz.AddFloor(mallArea))
                {
                    icon = "success";
                    msg = "保存成功。";
                }
                else
                    msg = "保存失败，同一停车场存在相同的楼层信息。";
            }
            ViewBag.ParkingName = parking.Name;
            ViewBag.Message = msg;
            ViewBag.Icon = icon;
            return View(mallArea);
        }

        public ActionResult EditFloor(int id)
        {
            var floor = _parkingBiz.GetMallArea(id);
            if (floor == null)
                return Redirect("/parkingAreas/parkings");
            ViewBag.ParkingName = _parkingBiz.GetSingle(floor.ParkingId).Name;
            return View(floor);
        }

        [HttpPost]
        public ActionResult EditFloor(MallArea mallArea)
        {
            var floor = _parkingBiz.GetMallArea(mallArea.Id);
            if (floor == null)
                return Redirect("/parkingAreas/parkings");

            string msg = string.Empty;
            string icon = "error";

            if (mallArea.Name.IsNullOrEmpty())
                msg = "楼层名称不能为空；";
            if (mallArea.ImgUrl.IsNullOrEmpty())
                msg += "图片不能为空。";
            if (msg.Length == 0)
            {
                mallArea.ImgUrl = FileHelper.Save("Areas", mallArea.ImgUrl);
                if (_parkingBiz.EditFloor(mallArea))
                {
                    icon = "success";
                    msg = "保存成功。";
                }
                else
                    msg = "保存失败，同一停车场存在相同的楼层信息。";
            }
            ViewBag.ParkingName = _parkingBiz.GetSingle(floor.ParkingId).Name;
            ViewBag.Message = msg;
            ViewBag.Icon = icon;
            return View(mallArea);
        }

        public ActionResult Floors(string name, int parkingId = 0, int page = 1)
        {
            Expression<Func<MallArea, bool>> exp = u => true;
            if (!name.IsNullOrEmpty())
                exp = exp.And(u => u.Name.ToLower().Contains(name));
            if (parkingId > 0)
            {
                exp = exp.And(u => u.ParkingId == parkingId);
                ViewBag.ParkingName = _parkingBiz.GetSingle(parkingId).Name;
            }
            var param = new PaginationParam<MallArea>()
            {
                PageIndex = page,
                PageSize = 20,
                Expression = exp,
                OrderByList = new List<OrderBy<MallArea>>() { new OrderBy<MallArea> { Sort = false, exp = u => u.Id } }
            };
            ViewBag.ParkingId = parkingId;
            return View(_parkingBiz.GetMallList(param));
        }

        public ActionResult FileUpLoadFile(string folderName)
        {
            var result = FileHelper.Upload(folderName);
            return Content(JsonConvert.SerializeObject(result));
        }
        #endregion
    }
}